<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>信息输入界面</title>
    <style>
        /* 页面基本样式 */
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f7f9fc;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
        }

        .container {
            background-color: #fff;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 500px;
            text-align: center;
            box-sizing: border-box;
        }

        h2 {
            color: #333;
            margin-bottom: 1rem;
            font-size: 24px;
        }

        h1 {
            font-size: 18px;
            color: #333;
            margin: 20px 0;
        }

        .input-group {
            margin-bottom: 1.5rem;
            text-align: left;
        }

        .input-group label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 600;
            color: #555;
        }

        .input-group input {
            width: 100%;
            padding: 0.8rem;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 16px;
            box-sizing: border-box;
        }

        .input-group input:focus {
            border-color: #4CAF50;
            outline: none;
        }

        .submit-btn {
            width: 100%;
            padding: 0.8rem;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s ease;
            margin-top: 1rem;
        }

        .submit-btn:hover {
            background-color: #45a049;
        }

        .submit-btn:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }

        .upload-btn {
            width: auto;
            padding: 0.6rem 1rem;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.3s ease;
            margin-top: 10px;
        }

        .upload-btn:hover {
            background-color: #45a049;
        }

        #status {
            margin-top: 10px;
            font-size: 16px;
            color: #555;
        }

        .progress-bar {
            width: 100%;
            max-width: 300px;
            background-color: #ddd;
            border-radius: 5px;
            overflow: hidden;
            margin-top: 20px;
        }

        .progress {
            width: 0;
            height: 10px;
            background-color: #4CAF50;
            transition: width 0.4s ease;
        }

        #uploadedImage, #uploadedVideo {
            margin-top: 20px;
            font-size: 14px;
            color: #333;
        }

        .message {
            margin-top: 1rem;
            font-weight: bold;
            color: #d9534f;
        }
    </style>
</head>
<body>

<div class="container">
    <h2>信息输入界面</h2>
<!--    <div class="input-group">-->
<!--        <label for="database">组名</label>-->
<!--        <input type="number" id="database" placeholder="例如: 0">-->
<!--    </div>-->
    <div class="input-group">
        <label for="key">手机名</label>
        <input type="text" id="key" placeholder="请输入手机名">
    </div>
    <div class="input-group">
        <label for="value">待发内容</label>
        <input type="text" id="value" placeholder="请输入待发送的内容">
    </div>
    <div class="input-group">
        <label for="qun">要转发的群聊</label>
        <input type="text" id="qun" placeholder="请输入群聊名称">
    </div>
    <button class="submit-btn" onclick="setKey()">提交</button>
    <div class="message" id="message"></div>

    <h1>文件上传</h1>

    <!-- 图片上传 -->
    <input type="file" id="fileInput" accept="image/png, image/jpeg, image/jpg, image/gif"/>
    <button class="upload-btn" id="uploadBtn">上传图片</button>

    <!-- 视频上传 -->
    <input type="file" id="videoInput" accept="video/mp4, video/mov"/>
    <button class="upload-btn" id="uploadVideoBtn">上传视频</button>

    <p id="status"></p>

    <!-- 进度条 -->
    <div class="progress-bar">
        <div class="progress" id="progress"></div>
    </div>

    <!-- 显示上传结果 -->
    <div id="uploadedImage"></div>
    <div id="uploadedVideo"></div>
</div>

<script>
    // 处理文本数据的提交
    async function setKey() {
        const submitBtn = document.querySelector('.submit-btn');
        // const database = document.getElementById('database').value;
        const key = document.getElementById('key').value;
        const value = document.getElementById('value').value;
        const qun = document.getElementById('qun').value;

        submitBtn.disabled = true;

        if (!key || !value) {
            document.getElementById('message').innerText = '请填写所有字段';
            setTimeout(() => submitBtn.disabled = false, 3000);
            return;
        }

        const response = await fetch('/set_key', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ key, value, qun })
        });

        const result = await response.json();
        if (result.status === 'success') {
            document.getElementById('message').innerText = '数据已成功存储到 Redis';
            setTimeout(() => document.getElementById('message').innerText = '', 3000);
        } else {
            document.getElementById('message').innerText = '发生错误: ' + result.message;
        }
        setTimeout(() => submitBtn.disabled = false, 3000);
    }

    // 处理图片上传
    const uploadBtn = document.getElementById('uploadBtn');
    const fileInput = document.getElementById('fileInput');
    const statusText = document.getElementById('status');
    const uploadedImage = document.getElementById('uploadedImage');
    const progressBar = document.getElementById('progress');

    uploadBtn.addEventListener('click', async () => {
        const file = fileInput.files[0];
        if (!file) {
            statusText.textContent = '请选择要上传的图片';
            return;
        }

        if (file.size > 10 * 1024 * 1024) {
            statusText.textContent = '文件大小超过10MB限制';
            return;
        }

        const formData = new FormData();
        formData.append('image', file);

        try {
            statusText.textContent = '上传中...';
            progressBar.style.width = '0%';

            const response = await fetch('http://116.62.120.28/upload', {
                method: 'POST',
                body: formData,
            });

            const result = await response.json();
            if (response.ok) {
                statusText.textContent = '上传成功!';
                progressBar.style.width = '100%';
                uploadedImage.innerHTML = `<p>上传的图片地址: ${result.file_path}</p>`;
            } else {
                statusText.textContent = `错误: ${result.error}`;
                progressBar.style.width = '0%';
            }
        } catch (error) {
            statusText.textContent = `错误: ${error.message}`;
            progressBar.style.width = '0%';
        }
    });

    // 处理视频上传
    const uploadVideoBtn = document.getElementById('uploadVideoBtn');
    const videoInput = document.getElementById('videoInput');
    const uploadedVideo = document.getElementById('uploadedVideo');

    uploadVideoBtn.addEventListener('click', async () => {
        const file = videoInput.files[0];
        if (!file) {
            statusText.textContent = '请选择要上传的视频';
            return;
        }

        const allowedFormats = ['video/mp4', 'video/mov'];
        if (!allowedFormats.includes(file.type)) {
            statusText.textContent = '只允许上传MP4或MOV格式的视频';
            return;
        }

        if (file.size > 50 * 1024 * 1024) { // 视频大小限制为50MB
            statusText.textContent = '视频文件大小超过50MB限制';
            return;
        }

        const formData = new FormData();
        formData.append('image', file);

        try {
            statusText.textContent = '视频上传中...';
            progressBar.style.width = '0%';

            const response = await fetch('http://116.62.120.28/upload', {
                method: 'POST',
                body: formData,
            });

            const result = await response.json();
            if (response.ok) {
                statusText.textContent = '视频上传成功!';
                progressBar.style.width = '100%';
                uploadedVideo.innerHTML = `<p>上传的视频地址: ${result.file_path}</p>`;
            } else {
                statusText.textContent = `错误: ${result.error}`;
                progressBar.style.width = '0%';
            }
        } catch (error) {
            statusText.textContent = `错误: ${error.message}`;
            progressBar.style.width = '0%';
        }
    });
</script>
</body>
</html>
